package com.qf.forumproject.download.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.forumproject.entity.QfDownload;
import com.qf.forumproject.utils.*;
import com.sun.javafx.scene.shape.PathUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import com.qf.forumproject.download.service.QfDownloadService;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.CollectionUtils;
import springfox.documentation.spring.web.json.Json;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

/**
 * (QfDownload)控制层
 *
 * @author protagonist
 * @since 2022-07-23 11:43:16
 */
@RestController
@Slf4j
@RequestMapping("/qfDownload")
@Api(tags = "下载专栏接口")
public class QfDownloadController {
    /**
     * 服务对象
     */
    @Resource
    private QfDownloadService qfDownloadServiceImpl;

    /**
     * 查询全部
     * @return Result对象
     *//*
    @GetMapping(value = "/selectAll")
    @ApiOperation("查询所有文件")
    public Result selectAll(SearchVo searchVo) {
        PageInit.initPage(searchVo);
        List<QfDownload> qfDownloads = qfDownloadServiceImpl.selectAll(searchVo);

        if (CollectionUtils.isEmpty(qfDownloads)) {
            return new Result<>(true,StatusCode.ERROR,"全部数据查询失败");
        }
        return new Result<>(true,StatusCode.OK,"全部数据查询成功",qfDownloads);
    }*/

    @GetMapping("/selectAll")
    @ApiOperation("查询所有文件")
    public Result selectAll(){
        List<QfDownload> list = qfDownloadServiceImpl.list();
        if (CollectionUtils.isEmpty(list)){
            return new Result<>(false,StatusCode.ERROR,"全部数据查询失败");
        }
        return new Result<>(true,StatusCode.OK,"全部数据查询成功",list);
    }

    /**
     * 下载栏按条件模糊查询
     * @return
     */
    @GetMapping("/vagueSelect")
    @ApiOperation("按条件模糊查询")
    public Result vagueSelection(SearchVo searchVo){
        //初始化分页参数
        //PageInit.initPage(searchVo);

        List<QfDownload> qfDownloads = qfDownloadServiceImpl.vagueSelect(searchVo);
        if (CollectionUtils.isEmpty(qfDownloads)) {
            return new Result<>(false,StatusCode.ERROR,"模糊查询失败");
        }
        return new Result<>(true,StatusCode.OK,"模糊查询成功",qfDownloads);
    }

    /**
     * 按文件标签类别查询文件
     * @param searchVo
     * @return
     */
    @GetMapping(value = "/selectByCategoryName")
    @ApiOperation("按条件查询某一类文件")
    public Result selectByCategoryName(SearchVo searchVo){
        //PageInit.initPage(searchVo);
        List<QfDownload> qfDownloads = qfDownloadServiceImpl.selectList(searchVo);

        if (CollectionUtils.isEmpty(qfDownloads)) {
            return new Result<>(false,StatusCode.ERROR,"按类名数据查询失败");
        }
        return new Result<>(true,StatusCode.OK,"按类名数据查询成功",qfDownloads);
    }

    /**
     * 文件下载接口
     * @param id
     * @param response
     */
    @GetMapping("/download")
    @ApiOperation(value = "文件下载接口")
    public void download(@ApiParam("文件id") Integer id, HttpServletResponse response){
        QfDownload qfFile = qfDownloadServiceImpl.getById(id);
        String filename = qfFile.getDname();
        String path = qfFile.getDdesc();

        try {
            response.addHeader("content-disposition","attachment;filename="+filename);
            IOUtils.copy(new FileInputStream(path),response.getOutputStream());
            log.debug("下载文件成功:filepath={}",path+filename);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
}
